<template>
	<view class="page">
		<!-- 地址列表 -->
		<view class="address-list">
			<view class="list" v-for="(item,index) in addressList" :key="index"  @tap="useAddress(item)">
				<view class="name-phone">
					<view class="name">
						<text class="one-omit">{{item.consignee}}</text>
					</view>
					<view class="phone">
						<text>{{item.phone}}</text>
						<text class="tag" v-if="item.is_default === 1">默认</text>
						<text class="tag blue">{{item.tag}}</text>
					</view>
				</view>
				<view class="address-edit">
					<view class="address">
						<text>{{item.area_text}}{{item.address}}</text>
					</view>
					<view class="edit" @click.stop="onAddressEdit(item.id)">
						<text class="iconfont icon-edit1"></text>
					</view>
				</view>
			</view>
		</view>
		<!-- 添加地址 -->
		<view class="add-address">
			<view class="btn" @click="onAddressEdit(0)">
				<text>新建收货地址</text>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				addressList: [],//收货地址
			};
		},
		onShow() {
			this.getAddressList()
		},
		methods:{
			//获取收货地址数据
			getAddressList(){
				this.$api.order.getAddress().then((res) => {
					// console.log(res)
					res.data.forEach((item,index)=>{
						item.area_text = item.area_text.replace(/-/g,'');
					})
					this.addressList = res.data
				}).catch((err) => {
					console.log('request fail', err);
				})
			},
			//返回上一页传递数据
			useAddress(address) {
				let pages = getCurrentPages();

				// #ifdef MP-WEIXIN || APP-PLUS
				let currPage = pages[pages.length - 1].$vm;
				let prevPage = pages[pages.length - 2].$vm; //上一个页面
				// #endif

				// #ifdef H5
				let currPage = pages[pages.length - 1];
				let prevPage = pages[pages.length - 2]; //上一个页面
				// #endif
				prevPage.address = address;

				uni.navigateBack({
					delta: 1
				});
			},
			/**
			 * 编辑地址点击
			 */
			onAddressEdit(id){
				uni.navigateTo({
					url: '/pages/AddressEdit/AddressEdit?id=' + id,
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	@import 'AddressList.scss';
</style>
